
package keter.mongo.model;

import java.util.List;

import keter.framework.util.ModelUtil;
import keter.mongo.base.MongoBase;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.jfinal.kit.JsonKit;

public class Book extends MongoBase<Book> {
	/**long serialVersionUID: TODO属性声明*/
	private static final long serialVersionUID = 1L;

	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(Book.class);
	
	public static final Book dao = new Book();
	
    private List<Author> authors = Lists.newArrayList(); 
        
    /**String AUTHOR_REF: 作者属性标识*/
    private static final String AUTHOR_PROP = "authors";

    public void addAuthor(Author r){
        this.authors.add(r);
        this.set(AUTHOR_PROP, JsonKit.toJson(authors));
    }
    
    public void remoleAuthor(Author r){
        this.authors.remove(r);
        this.set(AUTHOR_PROP, JsonKit.toJson(authors));
    }
    
    public List<Author> getAuthors() {
        String jsonStr = this.get(AUTHOR_PROP).toString();
        JSONArray jarr =  JSON.parseArray(jsonStr);
        List<Author> roles = Lists.newArrayList();
        for (Object object : jarr) {
            Author r = new Author();
            try {
                r.setColumns(ModelUtil.getMap(object.toString()));
            } catch (Exception e) {
                logger.error("",e);
            }
            roles.add(r);
        }
        return roles;
    }

    public Book findByName(String name) throws Exception {
    	 filter.put("name", name);
         return findOne(filter);
    }

//    @Override
//    public String getType() {
//        return "book";
//    }

}
